#! /bin/bash

create_dir(){
    mkdir -p data/{grafana,loki}
    sudo chmod -R 777 ./data
}

# 提前创建docker 网络
docker_netword(){
    if [ "$1" == "create" ]
    then
        docker network  create --driver bridge --subnet 192.168.10.0/24 --gateway 192.168.10.1 loki
    elif [ "$1" == "delete" ]
    then
        docker network rm loki > /dev/null
    fi
}

# 启动函数
main(){
    docker-compose  ps | grep grafana > /dev/null
    if [ $? != 0 ]
    then
        create_dir
        docker_netword create
        docker-compose up -d
        clear
        docker-compose ps
    else
        echo "Confirm to stop the current service!  yes | no"
        read -p "> "  selet
        case $selet in
        y | yes)
            docker-compose down
            docker_netword delete
            ;;
        n | no)
            exit 0
            ;;
        *)
            echo "请输入 yes | no "
            ;;
        esac
    fi
}

main